# !/usr/bin/env python
# -*-coding:utf-8 -*-
"""
# File       : task_schema.py
# Time       ：2023/7/7 21:56
# Author     ：Y-aong
# version    ：python 3.7
# Description：
    任务模型序列化类
    Task model serialization class
"""
from marshmallow import fields
from marshmallow_sqlalchemy import auto_field, SQLAlchemyAutoSchema

from apis.orderlines.models import Task, TaskInstance
from public.utils.schema_utils import CustomNested, columns_to_json
from public.utils.datetime_utils import calculate_time_difference


class TaskInstanceSchema(SQLAlchemyAutoSchema):
    task_id = auto_field()
    process_id = auto_field()
    process_instance_id = auto_field()
    start_time = fields.DateTime(format="%Y-%m-%d %H:%M:%S")
    end_time = fields.DateTime(format="%Y-%m-%d %H:%M:%S")

    class Meta:
        model = TaskInstance


class TaskSchema(SQLAlchemyAutoSchema):
    insert_time = fields.DateTime(format="%Y-%m-%d %H:%M:%S")
    update_time = fields.DateTime(format="%Y-%m-%d %H:%M:%S")
    process_id = auto_field()
    task_instance = CustomNested(TaskInstanceSchema, many=True, dump_only=True)

    class Meta:
        model = Task

class TaskODSchema(SQLAlchemyAutoSchema):
    insert_time = fields.DateTime(format="%Y-%m-%d %H:%M:%S")
    update_time = fields.DateTime(format="%Y-%m-%d %H:%M:%S")
    process_id = auto_field()

    class Meta:
        model = Task


class TaskInstanceExportSchema(SQLAlchemyAutoSchema):
    task_id = fields.String()
    task_instance_id = fields.String()
    task_name = fields.String()
    method_name = fields.String()
    task_status = fields.String()
    logs = fields.String()
    task_desc = fields.Function(serialize=lambda obj: obj.task_desc if obj.task_desc else '——')
    method_kwargs = fields.Function(serialize=lambda obj: columns_to_json(obj, 'method_kwargs', '——'))
    task_config = fields.Function(serialize=lambda obj: columns_to_json(obj, 'task_config','——'))
    task_error_info = fields.Function(serialize=lambda obj: columns_to_json(obj, 'task_error_info', '——'))
    task_result = fields.Function(serialize=lambda obj: columns_to_json(obj, 'task_result', '——'))
    start_time = fields.DateTime(format="%Y-%m-%d %H:%M:%S")
    end_time = fields.DateTime(format="%Y-%m-%d %H:%M:%S")
    elapsed = fields.Function(serialize=lambda obj: calculate_time_difference(obj.end_time, obj.start_time))


class TaskNodesSchema(SQLAlchemyAutoSchema):
    insert_time = fields.DateTime(format="%Y-%m-%d %H:%M:%S")
    update_time = fields.DateTime(format="%Y-%m-%d %H:%M:%S")
    process_id = auto_field()

    class Meta:
        model = Task


class TaskInstanceLogSchema(SQLAlchemyAutoSchema):
    task_name = fields.String()
    task_status = fields.String()
    method_kwargs = fields.Dict()
    task_config = fields.Dict()
    task_result = fields.Function(serialize=lambda obj: columns_to_json(obj, 'task_result'))
    task_error_info = fields.Function(serialize=lambda obj: columns_to_json(obj, 'task_error_info'))
    start_time = fields.DateTime(format="%Y-%m-%d %H:%M:%S")
    end_time = fields.DateTime(format="%Y-%m-%d %H:%M:%S")
